package be.novelfaces.component.util;


import static org.fest.assertions.Assertions.assertThat;

import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;

import org.junit.Test;

import be.novelfaces.test.common.AbstractMockFacesTestCase;

public class RenderUtilsImpl_isDisabledOrReadOnly_Test_ extends AbstractMockFacesTestCase<UIOutput> {

	private RenderUtilsImpl renderUtilsImpl;
	private boolean isDisabledCalled;
	private boolean disabled;
	private boolean isReadOnlyCalled;
	private boolean readOnly;
	private UIComponent calledIsDisabledUiComponent;
	private UIComponent calledIsReadOnlyUiComponent;
	
	@Override
	protected void extraSetUp() {
		renderUtilsImpl = new RenderUtilsImpl() {

			@Override
			public boolean isDisabled(UIComponent component) {
				calledIsDisabledUiComponent = component;
				isDisabledCalled = true;
				return disabled;
			};
			
			@Override
			public boolean isReadOnly(UIComponent component) {
				calledIsReadOnlyUiComponent = component;
				isReadOnlyCalled = true;
				return readOnly;
			};
		};
	}
	
	@Test 
	public void shouldOnlyCallIsDisabledWhenDisabled(){
		disabled = true;
		getEnvironment().replay();
		
		assertThat(renderUtilsImpl.isDisabledOrReadOnly(getUiComponentMock())).isTrue();
		assertThat(isDisabledCalled).isTrue();
		assertThat(isReadOnlyCalled).isFalse();
		assertThat(calledIsDisabledUiComponent).isSameAs(getUiComponentMock());
	}
	
	@Test 
	public void shouldCallIsDisabledAndReadOnlyAndReturnTrueWhenReadOnly(){
		disabled = false;
		readOnly = true;
		getEnvironment().replay();
		
		assertThat(renderUtilsImpl.isDisabledOrReadOnly(getUiComponentMock())).isTrue();
		assertThat(isDisabledCalled).isTrue();
		assertThat(isReadOnlyCalled).isTrue();
		assertThat(calledIsDisabledUiComponent).isSameAs(getUiComponentMock());
		assertThat(calledIsReadOnlyUiComponent).isSameAs(getUiComponentMock());
	}
	
	@Test 
	public void shouldCallIsDisabledAndReadOnlyAndReturnFalseWhenNotReadOnly(){
		disabled = false;
		readOnly = false;
		getEnvironment().replay();
		
		assertThat(renderUtilsImpl.isDisabledOrReadOnly(getUiComponentMock())).isFalse();
		assertThat(isDisabledCalled).isTrue();
		assertThat(isReadOnlyCalled).isTrue();
		assertThat(calledIsDisabledUiComponent).isSameAs(getUiComponentMock());
		assertThat(calledIsReadOnlyUiComponent).isSameAs(getUiComponentMock());
	}
	
	@Override
	protected Class<UIOutput> getUIComponentClass() {
		return UIOutput.class;
	}
	
}
